﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;

namespace SVAP
{
    /// <summary>
    /// WinAPI 的摘要说明。
    /// </summary>

    /// <summary>
    /// 设置窗体一直占有焦点，直至该窗体被关闭
    /// </summary>
    public class SetWindow
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern IntPtr GetForegroundWindow();   //WINAPI 获取当前活动窗体的句柄
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern bool SetForegroundWindow(IntPtr hWnd); //WINAPI 设置当前活动窗体的句柄



        private Thread Th;
        private IntPtr MainInt;

        public SetWindow(IntPtr MainIntPrt)
        {
            MainInt = MainIntPrt;
        }
        /// <summary>
        /// 开始线程
        /// </summary>
        public void Star()
        {
            Th = new Thread(new ThreadStart(SetForm));
            Th.Start();
        }
        private void SetForm()
        {
            while (true)
            {
                if (MainInt != GetForegroundWindow()) SetForegroundWindow(MainInt);

                Thread.Sleep(1);
            }
        }
        /// <summary>
        /// 关闭线程
        /// </summary>
        public void Close()
        {
            Th.Abort();
        }

    }
}
